package ${package_controller};
import ${package_pojo}.${Table};
import ${package_service}.${Table}Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import ${package_pojo}.Result;
import ${package_pojo}.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.v3.oas.annotations.*;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.*;

import java.util.Arrays;
import java.util.List;


<#if swagger==true>@Tag(name = "${Table}接口文档")</#if>
@RestController
@RequestMapping("/${table}")
@CrossOrigin
public class ${Table}Controller {

    @Autowired
    private ${Table}Service ${table}Service;

    /***
     * 条件分页查询
     * @param ${table}
     * @param page
     * @param size
     * @return
     */
    <#if swagger==true>
    @Operation(summary = "条件分页查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    </#if>
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@RequestBody(required = false) <#if swagger==true>@Parameter(name = "${Table}对象",required = true)</#if> ${Table} ${table}, @PathVariable(value="page")  int page, @PathVariable(value="size")  int size){
        //调用${Table}Service实现分页条件查询${Table}
        Page<${Table}> pageInfo = ${table}Service.page(new Page<${Table}>(page,size),new QueryWrapper<${Table}>(${table}));
        return new Result<Page>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 分页查询
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    <#if swagger==true>
    @Operation(summary = "分页查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    </#if>
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@PathVariable(value="page")  int page, @PathVariable(value="size")  int size){
        //调用${Table}Service实现分页查询${Table}
        Page<${Table}> pageInfo = ${table}Service.page(new Page<>(page,size));
        return new Result<Page>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 条件查询
     * @param ${table}
     * @return
     */
    <#if swagger==true>
    @Operation(summary = "条件查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    </#if>
    @PostMapping(value = "/search" )
    public Result<List<${Table}>> findList(@RequestBody(required = false) <#if swagger==true>@Parameter(name = "${Table}对象",required = true)</#if> ${Table} ${table}){
        //调用${Table}Service实现条件查询${Table}
        List<${Table}> list = ${table}Service.list(new QueryWrapper<${Table}>(${table}));
        return new Result<List<${Table}>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据Id删除${table}数据
     * @param id
     * @return
     */
    <#if swagger==true>
    @Operation(summary = "根据ID删除${Table}",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameter(in = ParameterIn.PATH, name = "id", description = "删除对象ID", required = true)
    </#if>
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable(value="id") ${keyType} id){
        //调用${Table}Service实现根据ID删除
        ${table}Service.removeById(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 根据Id批量删除${table}数据
     * @param ids
     * @return
     */
    <#if swagger==true>
    @Operation(summary = "根据Id批量删除${Table}",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    </#if>
    @DeleteMapping
    public Result deleteByIds(@RequestBody <#if swagger==true>@Parameter(name = "需要删除的${Table}对象id集合",required = true)</#if> ${keyType}[] ids){
        //调用${Table}Service实现根据ID批量删除
        ${table}Service.removeByIds(Arrays.asList(ids));
        return new Result(true,StatusCode.OK,"删除成功");
    }


    /***
     * 根据ID修改${Table}数据
     * @param ${table}
     * @return
     */
    <#if swagger==true>
    @Operation(summary = "根据ID修改${Table}",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    </#if>
    @PutMapping
    public Result updateById(@RequestBody <#if swagger==true>@Parameter(name = "${Table}对象",required = true)</#if> ${Table} ${table}){
        //调用${Table}Service实现修改${Table}
        ${table}Service.updateById(${table});
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增${Table}数据
     * @param ${table}
     * @return
     */
    <#if swagger==true>
    @Operation(summary = "添加${Table}",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    </#if>
    @PostMapping
    public Result add(@RequestBody  <#if swagger==true>@Parameter(name = "${Table}对象",required = true)</#if> ${Table} ${table}){
        //调用${Table}Service实现添加${Table}
        ${table}Service.save(${table});
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询${Table}数据
     * @param id
     * @return
     */
    <#if swagger==true>
    @Operation(summary = "根据ID查询${Table}",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameter(in = ParameterIn.PATH, name = "id", description = "查询对象ID", required = true)
    </#if>
    @GetMapping("/{id}")
    public Result<${Table}> findById(@PathVariable(value="id") ${keyType} id){
        //调用${Table}Service实现根据ID查询${Table}
        ${Table} ${table} = ${table}Service.getById(id);
        return new Result<${Table}>(true,StatusCode.OK,"查询成功",${table});
    }

    /***
     * 查询${Table}全部数据
     * @return
     */
    <#if swagger==true>
    @Operation(summary = "查询所有${Table}",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    </#if>
    @GetMapping
    public Result<List<${Table}>> findAll(){
        //调用${Table}Service实现查询所有${Table}
        List<${Table}> list = ${table}Service.list();
        return new Result<List<${Table}>>(true, StatusCode.OK,"查询成功",list) ;
    }



}
